require(['./config'], ()=>{
    require(['header','footer','jquery'], ()=>{
        class Index {
            constructor(){
            this.innt()
            }
            innt(){

                window.setCookie =function setCookie (key, value, option) {
                    var cookie = `${key}=${encodeURIComponent(value)}`
                    if (option) {
                        if (option.expires) {
                            var d = new Date()
                            d.setDate(d.getDate() + option.expires)
                            cookie += `;expires=${d.toUTCString()}`
                        }
                        if (option.path) {
                            cookie += `;path=${option.path}`
                        } else {
                            // 传了option但是不传path默认根路径
                            cookie += ';path=/'
                        }
                    } else {
                        // option没传，也让path默认根路径
                        cookie += ';path=/'
                    }
                    document.cookie = cookie
                }
                                   
                document.querySelector('.denglu_btn').onclick = function () {
                    var username = document.querySelector('.username').value
                    var password = document.querySelector('.password').value
                    var userList = localStorage.getItem('userList')
                    if (userList) {
                        userList = JSON.parse(userList)
                        // 判断userList里是否存在一条数据跟输入的用户名密码相同
                        var isExist = userList.some(user => {
                            return user.username === username && user.password === password
                        })
                        if (isExist) {
                            setCookie('loginUser', username)
                            alert('登录成功，即将回到首页')
                            location.href = '../../index.html'
                        } else {
                            alert('用户名或者密码错误')
                        }
                    } else {
                        // 还没有用户
                        alert('请先注册')
                        location.replace('./register.html')
                    }
                }
            }

            getCookie (key) {
                var str = document.cookie
                var arr = str.split('; ')
                var cookies = arr.reduce((obj, item) => {
                    var subArr = item.split('=')
                    obj[subArr[0]] = decodeURIComponent(subArr[1])
                    return obj
                }, {})
                return cookies[key]
            }

        }
        new Index()
    })
})